package com.nytd.demo.controller;

import com.nytd.demo.entity.*;
import com.nytd.demo.service.BookInfoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookInfoService bookService;

    @RequestMapping("/getList")
    public List<BookInfo> getList(){
//        BookService bookService = new BookService();
        List<BookInfo> books = bookService.getList();
        return books;

    }
    @RequestMapping(value = "/addBook", produces = "application/json;charset=UTF-8")
    public String addBook(BookInfo bookInfo){
        log.info("接收信息: bookName={}, author={}, count={}, price={}, publish={}, status={}",
                bookInfo.getBookName(), bookInfo.getAuthor(), bookInfo.getCount(),
                bookInfo.getPrice(), bookInfo.getPublish(), bookInfo.getStatus());
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || bookInfo.getStatus() == null
                || !StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数输入不合法, 请重新输入";
        }
        try {
            Integer result = bookService.addBook(bookInfo);
            return "新增图书成功";
        } catch (Exception e) {
            log.error("添加失败", e);
            return e.getMessage();
        }
    }
    @RequestMapping("/getListByPage")
    public Result<PageResult> getListByPage(PageRequest pageRequest, HttpSession session){
        log.info("图书列表:{}", pageRequest);
//        String name = (String)session.getAttribute(Constants.SESSION_USER_NAME);
//        if (!StringUtils.hasLength(name)) {
//           return Result.unlogin();
//        }
        PageResult<BookInfo> pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/selectBookById")
    public BookInfo selectBookById(Integer bookId){
        log.info("查询id:{}", bookId);
        if (bookId == null || bookId <= 0) {
            return null;
        }
        BookInfo bookInfo = bookService.selectBookById(bookId);
        return bookInfo;
    }

    @RequestMapping(value = "/updateBook", produces = "application/json;charset=UTF-8")
    public String updateBookById(BookInfo bookInfo){
        log.info("更新图书信息:{}", bookInfo);
        Integer id = bookInfo.getId();
        if (id <= 0) {
            return "参数错误,请重新传输";
        }
        Integer result = bookService.updateBookById(bookInfo);
        if (result == 1 && result != null) {
            return "";
        } else {
            return "修改失败";
        }

    }
    //逻辑删除
    @RequestMapping(value = "/deleteBook", produces = "application/json;charset=UTF-8")
    public String deleteBookById(Integer bookId){
        log.info("删除bookId:{}", bookId);
        if (bookId <= 0) {
            return "参数不合法";
        }
        Integer result = bookService.deleteBookById(bookId);
        if (result == 1 && result != null) {
            return "";
        } else {
            return "修改失败";
        }

    }

    @RequestMapping(value = "/batchDeleteBook", produces = "application/json;charset=UTF-8")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书id:{}", ids);
        try {
            bookService.batchDeleteBook(ids);
            return "";
        } catch (Exception e) {
            log.error("批量删除异常:{}", e);
            return "批量删除异常";
        }
    }



}
